import { UaIpEntity } from "../../models/ua-ip.model";
import { UaUserDeckDto } from "../../models/ua-user-deck.model";
import { GlobalService } from "../../services/global.service";
import { UaIpService } from "../../services/ua-ip.service";
import { UaTierService } from "../../services/ua-tier.service";
import { UserDeckService } from "../../services/ua-user-deck.service";

// pages/home/home.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    FILE_HOST: GlobalService.STATIC_FILE_HOST,
    FILE_ASSETS_HOST: GlobalService.STATIC_ASSETS_HOST,
    tiers: [1, 2, 3],
    ipList: [] as UaIpEntity[],
    AD: GlobalService.STATIC_FILE_HOST + '/marketing/20241103-cover.png',
    showMultiTextAndTitle: false,
    confirmBtn: { content: '确定', variant: 'base' },
    actionText: '',
    value: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query: any) {
    wx.setNavigationBarTitle({
      title: 'UA真选组'
    });
    const scene = decodeURIComponent(query.scene);
    if (!!scene && scene !== 'undefined') {
      UserDeckService.findBySn(scene).then((deck: UaUserDeckDto) => {
        if (!!deck.ipId && !!deck.id) {
          wx.navigateTo({
            url: `/pages/user/user-deck-detail/user-deck-detail.page?ipId=${deck.ipId}&deckId=${deck.id}`,
          });
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().setData({
      value: '/pages/home/home'
    });
    UaIpService.fetchUaIpList().then((response: UaIpEntity[]) => {
      response.sort((a, b) => b.tag!.length - a.tag!.length)
      this.setData({
        ipList: response
      })
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  chooseIp(e: any) {
    const ipId = e.currentTarget.dataset.ip;
    wx.navigateTo({
      url: `/pages/home/ip-card-search/ip-card-search.page?ipId=${ipId}&display=true`,
    });
  },

  ap() {
    wx.navigateTo({
      url: `/pages/user/ap/ap.page`
    })
  },

  closeDialog() {
    this.setData({
      showMultiTextAndTitle: false
    })
  },

  changeHandle(e: any) {
    const { value } = e.detail;
    this.setData({
      value,
    });
  },

  focusHandle() {
    this.setData({
      actionText: '搜索',
    });
  },

  blurHandle() {
    this.setData({
      actionText: '',
    });
  },

  actionHandle() {
    let search = "";
    if (!!this.data.value) {
      search = this.data.value;
    }
    wx.navigateTo({
      url: `/pages/home/single-card-search/single-card-search.page?search=${search}&display=true`,
    });
    this.setData({
      value: '',
      actionText: '',
    });
  },

  search(e: any) {
    console.log(e.detail.value)
    const search = e.detail.value;
    wx.navigateTo({
      url: `/pages/home/single-card-search/single-card-search.page?search=${search}&display=true`,
    });
    this.setData({
      value: '',
      actionText: '',
    });
  },

  tier(e: any) {
    wx.navigateTo({
      url: `/pages/lobby/tier/tier.page?collection=${e.currentTarget.dataset['collection']}`
    })
  },

  timeline(e: any) {
    wx.navigateTo({
      url: `/pages/info/info?type=Timeline`
    })
  },


})